<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Shicai_mdl extends MY_Model {

	const DB_SHICAI = 'shicai_new';		//食材数据库
	const DB_CATEGORY = 'category';		//食材分类数据库
	const DB_SHICAI_XIANGKE = 'shicai_xiangke';		//食材相生相克数据库

    public function __construct()
    {
		parent::__construct();
		$this->table = 'shicai_new';
    }

    //获取食材内容
    public function get_shicai_by_id($id)
    {
    	$data = array();
		$this->db->select('*')->from(self::DB_SHICAI)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$data['time'] = date("Y-m-d h:i", $data['time']);
		}
		$query->free_result();
		
		return $data;
    }

    public function get_shicai_xiangke_by_title($title)
    {
    	$title = urldecode($title);
    	$title = addslashes($title);
    	$data = array();
		$this->db->select('*')->from(self::DB_SHICAI_XIANGKE)->where('title', $title)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
    }
    //获取食物相克列表
    public function get_shicai_xiangke_list($offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('title');
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_SHICAI_XIANGKE);
    }

    public function get_shicai_by_title($title)
    {
    	$title = urldecode($title);
    	$title = addslashes($title);
    	$data = array();
		$this->db->select('*')->from(self::DB_SHICAI)->where('title', $title)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$data['time'] = date("Y-m-d h:i", $data['time']);
		}
		$query->free_result();
		
		return $data;
    }

    public function get_shicai_by_alias($alias)
    {
    	$alias = urldecode($alias);
    	$alias = addslashes($alias);
    	$data = array();

    	$sql = "SELECT * FROM ".$this->db->dbprefix.self::DB_SHICAI." WHERE FIND_IN_SET('$alias', alias)"; 

    	@$query = $this->db->query($sql);
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$data['time'] = date("Y-m-d h:i", $data['time']);
		}
		$query->free_result();
		
		return $data;
    }

    //获取食材列表classid
    public function get_shicai_list($classid = NULL,$offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		if($classid && is_numeric($classid))
		{
			$this->db->where('classid', intval($classid));
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_SHICAI);
    }

    //获取分类pid获取所有子分类下食材
    public function get_shicai_list_by_pid($pid = 0,$offset = NULL, $limit = NULL)
    {
        $this->db->select('*');
		if($pid && is_numeric($pid))
		{
			$this->db->where('pid', intval($pid));
		}
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

        return $this->db->get(self::DB_SHICAI);
    }

    //获取时令食材
    public function get_shicai_season($limit = NULL)
    {
    	$this->load->helper('global');
		$this->db->select('*');
		$this->db->where('season', get_season());
		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}

        return $this->db->get(self::DB_SHICAI);
    }

    //获取食材分类 0 所有，pid获取大分类下子分类
    public function get_shicai_category($fatherId = 0,$offset = NULL, $limit = NULL)
    {
		$this->db->select('cid,cateName');
		$this->db->where('fatherId', intval($fatherId));
		$this->db->where('ctype', 0);

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

        return $this->db->get(self::DB_CATEGORY);
    }

    //通过id获取当前分类名称  参数2 等于1返回上级分类名称
    public function get_shicai_category_name($cid,$layer = NULL)
    {
    	$data = array();
		$this->db->select('cid,cateName,fatherId')->from(self::DB_CATEGORY)->where('cid', intval($cid))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
    }
    //搜索
    public function get_shicai_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		$this->db->like('title', $this->db->escape_like_str($keywords));

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_SHICAI);
    }
	//***********************后台专用*********************************************************************
	public function shicai_insert($arr)
	{
		return $this->db->insert(self::DB_SHICAI,$arr);
	}

	public function shicai_update($arr, $id)
	{
		return $this->db->update(self::DB_SHICAI, $arr, 'id = '.$id);
	}

	public function shicai_delete($id)
	{
		$ser_img = 'http://img.ichufang.cn/';
		$this->db->select('id,pic')->where('id', intval($id));
		$query = $this->db->get(self::DB_SHICAI)->row_array();
		if ($query)
		{
			$this->load->library('alioss');
			$img_path = str_replace($ser_img, '', $query['pic']);
		    if (file_exists('./uploads/'.$img_path)) @unlink('./uploads/'.$img_path);
		    $this->alioss->delete_object('ichufang',$img_path);
		    return $this->db->where('id',$query['id'])->delete(self::DB_SHICAI);
		}
	}
}

/* End of file shicai_model.php */
/* Location: ./application/models/shicai_model.php */